package com.ruoyi.web.controller.exe;
import com.ruoyi.common.core.domain.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName handleValidationExceptions
 * @Description TODO  手机号校验拦截返回错误信息
 * @Author Mr、Liu
 * @Date 2025/6/26 18:01
 * @Version 1.0
 */
@RestControllerAdvice
public class GlobalValidationExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity handleValidationExceptions(
            MethodArgumentNotValidException ex) {

        AtomicReference<String> errorMessage = new AtomicReference<>();
        ex.getBindingResult().getFieldErrors().forEach(error ->
                errorMessage.set(error.getDefaultMessage())
        );

        return new ResponseEntity().error(errorMessage.get());
    }
}
